library(knitr)
library(dplyr)
library(tidyverse)
library(ggplot2)
library(gridExtra)
library(magrittr)
library(plotly)
library(ggpubr)
library(patchwork)
library(ggmosaic)

0.1 Importing Data

df <- read.csv("Bidit_Life_data_tracking.csv",header = T, stringsAsFactors = T )

0.2 A short overview on my habits,health and mindset

0.2.1 Mindset,Mood & Focus

mindset <- data.frame(table(df$Mindset.Motivated.Yes.Lazy.No.))
basic_1 <-ggplot(mindset, aes(y=Freq, x=Var1)) +
  geom_bar(position="dodge", stat="identity",fill="#FFCC66")+theme_minimal() + geom_text(aes(label = Freq), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("No","Yes"))+xlab("Mindset is Positive")+ylab("Count")+ylim(0,113)


positivity <- data.frame(table(df$Positivity..1.worst.5.best.))
colnames(positivity) <- c("Mood_Rating","Count")
basic_2 <-ggplot(positivity, aes(y=Count, x=Mood_Rating)) +
  geom_bar(position="dodge", stat="identity",fill="#FFCC66")+theme_minimal() + geom_text(aes(label = Count), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("Worst","Bad","Moderate","Good","Too Good"))+xlab("Mood Rating")+ylab("Count")+ylim(0,113)

focus <- data.frame(table(df$Focus.Level..1.worst.5.best.))
focus_plot <- ggplot(focus, aes(y=Freq, x=Var1)) +
  geom_bar(position="dodge", stat="identity",fill="#FFCC66")+theme_minimal() + geom_text(aes(label = Freq), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("<10mins","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90mins"))+xlab("Focus Time")+ylab("Count")+ylim(0,113)


figure <- ggarrange(
  focus_plot,                # First row
  # Second Row
  ggarrange(basic_1, basic_2, ncol = 2, labels = c("Mindset", "Mood")), 
  nrow = 2, 
  labels = "Focus Time"       # Label of the plot
  ) 
annotate_figure(figure, top = text_grob("Visualizing Mood,Mindset & Focus Time",
                  color = "red", face = "bold", size = 14),
  bottom = text_grob("Data source:Bidit Personal Notion Database", color = "black",
                     hjust = 1, x = 1, face = "italic", size = 10),
  fig.lab = "Figure 1", fig.lab.face = "bold"
  )

0.2.2 Health

#exercise
exercise <- data.frame(table(df$Exercise))
colnames(exercise) <- c("Exercise","Frequency1")
exercise_plot <- ggplot(exercise, aes(y=Frequency1, x=Exercise)) +
  geom_bar(position="dodge", stat="identity",fill="#FF6666")+theme_minimal() + geom_text(aes(label = Frequency1), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("No","Yes"))+xlab("Exercise")+ylab("Count")+ylim(0,113)

#health
health_condition <- data.frame(table(df$Health.Condition..5.Best..1.worst.))
colnames(health_condition) <- c("Health","Frequency2")
health_condition_plot <- ggplot(health_condition, aes(y=Frequency2, x=Health)) +
  geom_bar(position="dodge", stat="identity",fill="#FF6666")+theme_minimal() + geom_text(aes(label = Frequency2), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("Extremely Sick","Sick","OK","Good","Healthy,Energetic"))+xlab("Health Condition")+ylab("Count")+ylim(0,113)

#diet calculations
diet <- data.frame(table(df$Diet..1..worst..to.5..best.))
colnames(diet) <- c("Diet","Frequency3")

diet_plot <- ggplot(diet, aes(y=Frequency3, x=Diet)) +
  geom_bar(position="dodge", stat="identity",fill="#FF6666")+theme_minimal() + geom_text(aes(label = Frequency3), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("Junk","Unbalanced","OK","Healthly","Balanced"))+xlab("Diet Level")+ylab("Count")+ylim(0,113)

#plotting
figure1 <- ggarrange(
  health_condition_plot,                # First row with line plot
  # Second row with box and dot plots
  ggarrange(exercise_plot, diet_plot, ncol = 2, labels = c("Exercise ", "Diet Levels")), 
  nrow = 2, 
  labels = "Health"       # Label of the line plot
  ) 
annotate_figure(figure1, top = text_grob("Visualizing Health,Diet & Exercise",
                  color = "red", face = "bold", size = 14),
  bottom = text_grob("Data source:Bidit Personal Notion Database", color = "black",
                     hjust = 1, x = 1, face = "italic", size = 10),
  fig.lab = "Figure 2", fig.lab.face = "bold"
  )

#water consumption calculation
# Creating the month coloumn
df$Month <- format(df$On.This.Day,"%b")
water_consumption <- data.frame(tapply(df$Water.Consumption.in.litre.,df$Month,mean))
df2 <- na.omit(df)   # omitting na values in workout time
water_grandmean <- round(mean(df$Water.Consumption.in.litre.),1)
water_consumption_plot <- ggplot(df,aes(x=Month, y=Water.Consumption.in.litre.,fill=Month))+geom_boxplot(alpha=0.3)+theme(legend.position = "none")+scale_fill_brewer(palette = "Dark2")+ylab(" Drinking Water Consumption (in litre)")+geom_hline(aes(yintercept=water_grandmean),colour="#6666CC",lwd=1)+geom_text(aes(0,water_grandmean,label= paste("Average/day",water_grandmean,sep = ":"),vjust=-1,hjust=0),colour="#6666FF")+stat_summary(fun = mean,geom="point",shape=20,size=5,color="red",fill="red")

#Workout calculation
workout_time <- data.frame(tapply(df2$Workout.time.in.mins.,df2$Month,mean))
workout_grandmean <- round(mean(df2$Workout.time.in.mins.),1)

workout_time_plot <- ggplot(df2,aes(x=Month, y=Workout.time.in.mins.,fill=Month))+geom_boxplot(alpha=0.3)+theme(legend.position = "none")+scale_fill_brewer(palette = "Dark2")+ylab("Workout Time (in hour)")+geom_hline(aes(yintercept=workout_grandmean),colour="#6666CC",lwd=1)+geom_text(aes(0,workout_grandmean,label= paste("Average/day",workout_grandmean,sep = ":"),vjust=-1,hjust=0),colour="#6666FF")+stat_summary(fun = mean,geom="point",shape=20,size=5,color="red",fill="red")

#sleep time calculation
sleep_grandmean <- round(mean(df$Sleep.Time.day),1)
sleep_time_plot <- ggplot(df,aes(x=Month, y=Sleep.Time.day,fill=Month))+geom_boxplot(alpha=0.3)+theme(legend.position = "none")+scale_fill_brewer(palette = "Dark2")+ylab("Sleeping Time(in hour)")+geom_hline(aes(yintercept=sleep_grandmean),colour="#6666CC",lwd=1)+geom_text(aes(0,sleep_grandmean,label= paste("Average/day",sleep_grandmean,sep = ":"),vjust=2,hjust=0),colour="#6666FF")+stat_summary(fun = mean,geom="point",shape=20,size=5,color="red",fill="red")

#plotting
figure2 <- ggarrange(
  water_consumption_plot,                # First row with line plot
  # Second row with box and dot plots
  ggarrange(workout_time_plot, sleep_time_plot, ncol = 2, labels = c("Workout Time", "Sleeping Time")), 
  nrow = 2, 
  labels = " Drinking Water Consumption"       # Label of the line plot
  ) 
annotate_figure(figure2, top = text_grob("Visualizing Daily Essential Needs & Activities",
                  color = "red", face = "bold", size = 14),
  bottom = text_grob("Data source:Bidit Personal Notion Database", color = "black",
                     hjust = 1, x = 1, face = "italic", size = 10),
  fig.lab = "Figure 2", fig.lab.face = "bold"
  )

0.2.3 Hobbies & Extra-Curricular Activities

#meditation
meditation <- data.frame(table(df$Meditation))
colnames(meditation) <- c("Medi","Frequencymed")
meditation_plot <- ggplot(meditation, aes(y=Frequencymed, x=Medi)) +
  geom_bar(position="dodge", stat="identity",fill="#CCFF00")+theme_minimal() + geom_text(aes(label = Frequencymed), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("No","Yes"))+xlab("Meditation")+ylab("Count")+ylim(0,113)

#reading
reading <- data.frame(table(df$Reading..Doesn.t.include.Studying.))
colnames(reading) <- c("Reading","Frequencyread")
reading_plot <- ggplot(reading, aes(y=Frequencyread, x=Reading)) +
  geom_bar(position="dodge", stat="identity",fill="#CCFF00")+theme_minimal() + geom_text(aes(label = Frequencyread), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("No","Yes"))+xlab("Reading")+ylab("Count")+ylim(0,113)

#morning prayer
prayer <- data.frame(table(df$Morning.Prayer))
colnames(prayer) <- c("Prayer","Frequencypray")
prayer_plot <- ggplot(prayer, aes(y=Frequencypray, x=Prayer)) +
  geom_bar(position="dodge", stat="identity",fill="#CCFF00")+theme_minimal() + geom_text(aes(label = Frequencypray), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("No","Yes"))+xlab("Morning Prayer")+ylab("Count")+ylim(0,113)

# Phone time
phone_grandmean <- round(mean(df$Phone.Time.in.mins.),1)
phone_time_plot <- ggplot(df,aes(x=Month, y=Phone.Time.in.mins.,fill=Month))+geom_boxplot(alpha=0.3)+theme(legend.position = "none")+scale_fill_brewer(palette = "Dark2")+ylab("Phone Usage Time(in hour)")+geom_hline(aes(yintercept=phone_grandmean),colour="#6666CC",lwd=1)+geom_text(aes(0,phone_grandmean,label= paste("Average/day",phone_grandmean,sep = ":"),vjust=1,hjust=0),colour="#6666FF")+stat_summary(fun = mean,geom="point",shape=20,size=5,color="red",fill="red")

#plotting
figure3 <- ggarrange(
  phone_time_plot,                # First row with line plot
  # Second row with box and dot plots
  ggarrange(meditation_plot, reading_plot, ncol = 2, labels = c("Meditation", "Reading")), 
  nrow = 2, 
  labels = " Phone Usage Time"       # Label of the line plot
  ) 
annotate_figure(figure3, top = text_grob("Visualizing Daily Essential Needs & Activities",
                  color = "red", face = "bold", size = 14),
  bottom = text_grob("Data source:Bidit Personal Notion Database", color = "black",
                     hjust = 1, x = 1, face = "italic", size = 10),
  fig.lab = "Figure 2", fig.lab.face = "bold"
  )

0.3 Activities,Health & Mindset on Focus Time

0.3.1 Meditation on Focus Time

q <- table(df$Focus.Level..1.worst.5.best.,df$Meditation)
q_prop <- round(prop.table(q,margin = 2),2)*100
p <- addmargins(q)
rownames(p) <- c("<=10min","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins","Sum")
t <- as.matrix(p)
z <- data.frame(q_prop)
colnames(z) <- c("Focus_Level","Meditation_Status","Count")

fig1 <- plot_ly(
  type = 'table',
  header = list(
    values = c("<b>Focussed Time</b>", "<b>Meditation:No</b>","<b>Meditation:Yes</b>","<b>Sum</b>"),
  align = c('left', rep('center', ncol(t))),
  line = list(width = 1, color = 'black'),
  fill = list(color = 'rgb(235, 100, 230)'),
  font = list(family = "Arial", size = 14, color = "white")
  ),
  cells = list(
    values = rbind(
      rownames(t), 
      t(as.matrix(unname(t)))
    ),
    align = c('left', rep('center', ncol(t))),
    line = list(color = "black", width = 1),
    fill = list(color = c('rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)')),
    font = list(family = "Arial", size = 12, color = c("black"))
  ))

fig1
plot_1 <-ggplot(z, aes(fill=Meditation_Status, y=Count, x=Focus_Level)) +
  geom_bar(position="dodge", stat="identity",colour="black")+theme_minimal() + geom_text(aes(label = paste(Count,"%",sep = "")), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("<=10 mins","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins"))+xlab("Focus Time")+ylab("Percentage")+labs(title="EFFECT OF MEDITATION ON FOCUS TIME",caption = "Data Source:Bidit Personal Notion Database",tag = "Figure-1",fill="Meditation Done")+coord_flip()
plot_1

0.3.2 Exercise on Focus Time

q1 <- table(df$Focus.Level..1.worst.5.best.,df$Exercise)
q1_prop <- round(prop.table(q1,margin = 2),2)*100
p1 <- addmargins(q1)
rownames(p1) <- c("<=10min","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins","Sum")
t1 <- as.matrix(p1)
z1 <- data.frame(q1_prop)
colnames(z1) <- c("Focus_Level","Exercise_Status","Count")

fig3 <- plot_ly(
  type = 'table',
  header = list(
    values = c("<b>Focussed Time</b>", "<b>Exercise:No</b>","<b>Exercise:Yes</b>","<b>Sum</b>"),
  align = c('left', rep('center', ncol(t1))),
  line = list(width = 1, color = 'black'),
  fill = list(color = 'rgb(235, 100, 230)'),
  font = list(family = "Arial", size = 14, color = "white")
  ),
  cells = list(
    values = rbind(
      rownames(t1), 
      t(as.matrix(unname(t1)))
    ),
    align = c('left', rep('center', ncol(t))),
    line = list(color = "black", width = 1),
    fill = list(color = c('rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)')),
    font = list(family = "Arial", size = 12, color = c("black"))
  ))

fig3
plot_2 <-ggplot(z1, aes(fill=Exercise_Status, y=Count, x=Focus_Level)) +
  geom_bar(position="dodge", stat="identity",colour="black")+theme_minimal() + geom_text(aes(label = paste(Count,"%",sep = "")), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("<=10 mins","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins"))+xlab("Focus Time")+ylab("Percentage")+labs(title="EFFECT OF EXERCISE ON FOCUS TIME",caption = "Data Source:Bidit Personal Notion Database",tag = "Figure-2",fill="Exercise Done")+coord_flip()
plot_2

0.3.3 Reading on Focus Time

q2 <- table(df$Focus.Level..1.worst.5.best.,df$Reading..Doesn.t.include.Studying.)
q2_prop <- round(prop.table(q2,margin = 2),2)*100
p2 <- addmargins(q2)
rownames(p2) <- c("<=10min","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins","Sum")
t2 <- as.matrix(p2)
z2 <- data.frame(q2_prop)
colnames(z2) <- c("Focus_Level","Reading_Status","Count")

fig3 <- plot_ly(
  type = 'table',
  header = list(
    values = c("<b>Focussed Time</b>", "<b>Reading:No</b>","<b>Reading:Yes</b>","<b>Sum</b>"),
  align = c('left', rep('center', ncol(t2))),
  line = list(width = 1, color = 'black'),
  fill = list(color = 'rgb(235, 100, 230)'),
  font = list(family = "Arial", size = 14, color = "white")
  ),
  cells = list(
    values = rbind(
      rownames(t2), 
      t(as.matrix(unname(t2)))
    ),
    align = c('left', rep('center', ncol(t2))),
    line = list(color = "black", width = 1),
    fill = list(color = c('rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)')),
    font = list(family = "Arial", size = 12, color = c("black"))
  ))

fig3
plot_3 <-ggplot(z2, aes(fill=Reading_Status, y=Count, x=Focus_Level)) +
  geom_bar(position="dodge", stat="identity",colour="black")+theme_minimal() + geom_text(aes(label = paste(Count,"%",sep = "")), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("<=10 mins","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins"))+xlab("Focus Time")+ylab("Percentage")+labs(title="EFFECT OF READING ON FOCUS TIME",caption = "Data Source:Bidit Personal Notion Database",tag = "Figure-3",fill="Reading Done")+coord_flip()
plot_3

0.3.4 Mindset on Focus time

q3 <- table(df$Focus.Level..1.worst.5.best.,df$Mindset.Motivated.Yes.Lazy.No.)
q3_prop <- round(prop.table(q3,margin = 2),2)*100
p3 <- addmargins(q3)
rownames(p3) <- c("<=10min","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins","Sum")
t3 <- as.matrix(p3)
z3 <- data.frame(q3_prop)
colnames(z3) <- c("Focus_Level","Mindset_Status","Count")

fig4 <- plot_ly(
  type = 'table',
  header = list(
    values = c("<b>Focussed Time</b>", "<b>Mindest Positive:No</b>","<b>Mindset Positive:Yes</b>","<b>Sum</b>"),
  align = c('left', rep('center', ncol(t2))),
  line = list(width = 1, color = 'black'),
  fill = list(color = 'rgb(235, 100, 230)'),
  font = list(family = "Arial", size = 14, color = "white")
  ),
  cells = list(
    values = rbind(
      rownames(t3), 
      t(as.matrix(unname(t3)))
    ),
    align = c('left', rep('center', ncol(t2))),
    line = list(color = "black", width = 1),
    fill = list(color = c('rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)')),
    font = list(family = "Arial", size = 12, color = c("black"))
  ))

fig4
plot_4 <-ggplot(z3, aes(fill=Mindset_Status, y=Count, x=Focus_Level)) +
  geom_bar(position="dodge", stat="identity",colour="black")+theme_minimal() + geom_text(aes(label = paste(Count,"%",sep = "")), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 4)+scale_x_discrete(labels=c("<=10 mins","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins"))+xlab("Focus Time")+ylab("Percentage")+labs(title="EFFECT OF MINDSET ON FOCUS TIME",caption = "Data Source:Bidit Personal Notion Database",tag = "Figure-4",fill="Mindset Positive")+coord_flip()
plot_4

0.3.5 Diet on Focus time

q4 <- table(df$Focus.Level..1.worst.5.best.,df$Diet..1..worst..to.5..best.)
q4_prop <- round(prop.table(q4,margin = 2),2)*100
p4 <- addmargins(q4)
rownames(p4) <- c("<=10min","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins","Sum")
t4 <- as.matrix(p4)
z4 <- data.frame(q4_prop)
colnames(z4) <- c("Focus_Level","Diet_Level","Count")

fig5 <- plot_ly(
  type = 'table',
  header = list(
    values = c("<b>Focussed Time</b>", "<b>Diet Levels:1</b>","<b>2</b>","<b>3</b>","<b>4</b>","<b>5</b>","<b>Sum</b>"),
  align = c('left', rep('center', ncol(t4))),
  line = list(width = 1, color = 'black'),
  fill = list(color = 'rgb(235, 100, 230)'),
  font = list(family = "Arial", size = 14, color = "white")
  ),
  cells = list(
    values = rbind(
      rownames(t4), 
      t(as.matrix(unname(t4)))
    ),
    align = c('left', rep('center', ncol(t4))),
    line = list(color = "black", width = 1),
    fill = list(color = c('rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)')),
    font = list(family = "Arial", size = 12, color = c("black"))
  ))

fig5
plot_5 <-ggplot(z4, aes(fill=Diet_Level, y=Count, x=Focus_Level)) +
  geom_bar(position="dodge", stat="identity",colour="black")+theme_minimal() + geom_text(aes(label = paste(Count,"%",sep = "")), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 3)+scale_x_discrete(labels=c("<=10 mins","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins"))+xlab("Focus Time")+ylab("Percentage")+labs(title="EFFECT OF DIET LEVEL ON FOCUS TIME",caption = "Data Source:Bidit Personal Notion Database",tag = "Figure-5",fill="Diet Level(1=Worst,5=Best)")+coord_flip()
plot_5
ggplot(data = df) +
  geom_mosaic(aes(x = product(Diet..1..worst..to.5..best., Focus.Level..1.worst.5.best.), fill = Diet..1..worst..to.5..best.),
              na.rm=TRUE) + 
  labs(x = "Focus Level", 
       y = "Proportion", 
       title="Focus Time by Diet Levels",
       subtitle = "Left - Right = (1) <10 mins - (5) >90 mins",
       caption = "Data Source:Bidit Notion Personal Database",
       tag="Fig-5") +
  scale_fill_manual(values=c("#440145FF", "#404788FF", "#238A8DFF", "#55C667FF", "#FDE725FF"), 
                    name="Diet Levels",
                    breaks=c("1", "2", "3", "4", "5"),
                    labels=c("1 - Worst", "2", "3", "4", "5 - Best")) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())

0.3.6 Health Condition on Focus Time

q5 <- table(df$Focus.Level..1.worst.5.best.,df$Health.Condition..5.Best..1.worst.)
q5_prop <- round(prop.table(q5,margin = 2),2)*100
p5 <- addmargins(q5)
rownames(p5) <- c("<=10min","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins","Sum")
t5 <- as.matrix(p5)
z5 <- data.frame(q5_prop)
colnames(z5) <- c("Focus_Level","Health_Condition","Count")

fig6 <- plot_ly(
  type = 'table',
  header = list(
    values = c("<b>Focussed Time</b>", "<b>Health Condition:1</b>","<b>2</b>","<b>3</b>","<b>4</b>","<b>5</b>","<b>Sum</b>"),
  align = c('left', rep('center', ncol(t5))),
  line = list(width = 1, color = 'black'),
  fill = list(color = 'rgb(235, 100, 230)'),
  font = list(family = "Arial", size = 14, color = "white")
  ),
  cells = list(
    values = rbind(
      rownames(t5), 
      t(as.matrix(unname(t5)))
    ),
    align = c('left', rep('center', ncol(t5))),
    line = list(color = "black", width = 1),
    fill = list(color = c('rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)')),
    font = list(family = "Arial", size = 12, color = c("black"))
  ))

fig6
plot_6 <-ggplot(z5, aes(fill=Health_Condition, y=Count, x=Focus_Level)) +
  geom_bar(position="dodge", stat="identity",colour="black")+theme_minimal() + geom_text(aes(label = paste(Count,"%",sep = "")), fontface = "bold", vjust = .5, position = position_dodge(0.9), size = 3)+scale_x_discrete(labels=c("<=10 mins","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins"))+xlab("Focus Time")+ylab("Percentage")+labs(title="EFFECT OF HEALTH CONDITION ON FOCUS TIME",caption = "Data Source:Bidit Personal Notion Database",tag = "Figure-6",fill="Health Condition(1=Worst,5=Best)")+coord_flip()
plot_6
ggplot(data = df) +
  geom_mosaic(aes(x = product(Health.Condition..5.Best..1.worst., Focus.Level..1.worst.5.best.), fill = Health.Condition..5.Best..1.worst.),
              na.rm=TRUE) + 
  labs(x = "Focus Level", 
       y = "Proportion", 
       title="Focus Time by Health Condition",
       subtitle = "Left - Right = (1) <10 mins - (5) >90 mins",
       caption = "Data Source:Bidit Notion Personal Database",
       tag="Fig-6") +
  scale_fill_manual(values=c("#440145FF", "#404788FF", "#238A8DFF", "#55C667FF", "#FDE725FF"), 
                    name="Health Condition",
                    breaks=c("1", "2", "3", "4", "5"),
                    labels=c("1 - Worst", "2", "3", "4", "5 - Best")) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())

0.3.7 Mood on Focus Time

q6 <- table(df$Focus.Level..1.worst.5.best.,df$Positivity..1.worst.5.best.)
q6_prop <- round(prop.table(q6,margin = 2),2)*100
p6 <- addmargins(q6)
rownames(p6) <- c("<=10min","10 to 30 mins","30 to 60 mins","60 to 90 mins",">90 mins","Sum")
t6 <- as.matrix(p6)
z6 <- data.frame(q6_prop)
colnames(z6) <- c("Focus_Level","Health_Condition","Count")

fig7 <- plot_ly(
  type = 'table',
  header = list(
    values = c("<b>Focussed Time</b>", "<b>Mood Rating:1</b>","<b>2</b>","<b>3</b>","<b>4</b>","<b>5</b>","<b>Sum</b>"),
  align = c('left', rep('center', ncol(t5))),
  line = list(width = 1, color = 'black'),
  fill = list(color = 'rgb(235, 100, 230)'),
  font = list(family = "Arial", size = 14, color = "white")
  ),
  cells = list(
    values = rbind(
      rownames(t6), 
      t(as.matrix(unname(t6)))
    ),
    align = c('left', rep('center', ncol(t6))),
    line = list(color = "black", width = 1),
    fill = list(color = c('rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)')),
    font = list(family = "Arial", size = 12, color = c("black"))
  ))

fig7
ggplot(data = df) +
  geom_mosaic(aes(x = product(Positivity..1.worst.5.best., Focus.Level..1.worst.5.best.), fill = Positivity..1.worst.5.best.),
              ) + 
  labs(x = "Focus Level", 
       y = "Proportion", 
       title="Focus Time by Mood",
       subtitle = "Left - Right = (1) <10 mins - (5) >90 mins",
       caption = "Data Source:Bidit Notion Personal Database",
       tag="Fig-6") +
  scale_fill_manual(values=c("#440145FF", "#404788FF", "#238A8DFF", "#55C667FF", "#FDE725FF"), 
                    name="Mood",
                    breaks=c("1","2","3","4","5"),
                    labels=c("1-Worst","2-Bad","3-Moderate","4-Good","5-Too Good")) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())